package com.sky.controller.user;


import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "C端购物车相关接口")
public class ShoppingCarController {

    @Autowired
    private ShoppingCarService shoppingCarService;

    /**
     * 添加购物车接口
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车接口")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车，商品信息为：{}", shoppingCartDTO);

        shoppingCarService.add(shoppingCartDTO);

        return Result.success();
    }


    /**
     * 查询购物车信息（通过当前用户）
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("通过当前用户id查询购物车数据")
    public Result<List<ShoppingCart>> list(){
        Long userId = BaseContext.getCurrentId();

        log.info("查询购物车数据，当前用户id为：{}", userId);

        List<ShoppingCart> shoppingCarts = shoppingCarService.listByUserId(userId);

        return Result.success(shoppingCarts);
    }


    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    @ApiOperation("通过当前用户id清空购物车数据")
    public Result delete(){
        Long userId = BaseContext.getCurrentId();

        log.info("清空购物车数据，当前用户id为：{}", userId);

        shoppingCarService.clean(userId);

        return Result.success();
    }


    /**
     * 在购物车中减少对应购物车数据
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/sub")
    @ApiOperation("在购物车中减少对应购物车数据")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("减去购物车数据：{}", shoppingCartDTO);

        shoppingCarService.sub(shoppingCartDTO);

        return Result.success();
    }
}
